package org.soc.timesheet.domain;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "TIMESHEET")
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "TIMESHEET")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "idSubProject", "idProject", "idUser" })
@RooJson
public class Timesheet {

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(generator = "alphanumeric")
    @GenericGenerator(name = "alphanumeric", strategy = "org.soc.timesheet.custom.domain.generators.RandomStringIdGenerator", parameters = { @org.hibernate.annotations.Parameter(name = "length", value = "100") })
    @Column(name = "ID", length = 100)
    private String id;

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("id");

    public static final EntityManager entityManager() {
        EntityManager em = new Timesheet().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countTimesheets() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Timesheet o", Long.class).getSingleResult();
    }

    public static List<Timesheet> findAllTimesheets() {
        return entityManager().createQuery("SELECT o FROM Timesheet o", Timesheet.class).getResultList();
    }

    public static List<Timesheet> findAllTimesheets(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Timesheet o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Timesheet.class).getResultList();
    }

    public static Timesheet findTimesheet(String id) {
        if (id == null || id.length() == 0) return null;
        return entityManager().find(Timesheet.class, id);
    }

    public static List<Timesheet> findTimesheetEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Timesheet o", Timesheet.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<Timesheet> findTimesheetEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Timesheet o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Timesheet.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Timesheet attached = Timesheet.findTimesheet(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public Timesheet merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Timesheet merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static Timesheet fromJsonToTimesheet(String json) {
        return new JSONDeserializer<Timesheet>().use(null, Timesheet.class).deserialize(json);
    }

    public static String toJsonArray(Collection<Timesheet> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<Timesheet> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    public static Collection<Timesheet> fromJsonArrayToTimesheets(String json) {
        return new JSONDeserializer<List<Timesheet>>().use("values", Timesheet.class).deserialize(json);
    }

    @ManyToOne
    @JoinColumn(name = "ID_SUB_PROJECT", referencedColumnName = "ID", nullable = false)
    private SubProjects idSubProject;

    @ManyToOne
    @JoinColumn(name = "ID_PROJECT", referencedColumnName = "ID", nullable = false)
    private Projects idProject;

    @ManyToOne
    @JoinColumn(name = "ID_USER", referencedColumnName = "ID", nullable = false)
    private Users idUser;

    @Column(name = "DATA")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "MM")
    private Calendar data;

    @Column(name = "START_HOUR")
    private Integer startHour;

    @Column(name = "START_MIN")
    private Integer startMin;

    @Column(name = "END_HOUR")
    private Integer endHour;

    @Column(name = "END_MIN")
    private Integer endMin;

    public SubProjects getIdSubProject() {
        return idSubProject;
    }

    public void setIdSubProject(SubProjects idSubProject) {
        this.idSubProject = idSubProject;
    }

    public Projects getIdProject() {
        return idProject;
    }

    public void setIdProject(Projects idProject) {
        this.idProject = idProject;
    }

    public Users getIdUser() {
        return idUser;
    }

    public void setIdUser(Users idUser) {
        this.idUser = idUser;
    }

    public Calendar getData() {
        return data;
    }

    public void setData(Calendar data) {
        this.data = data;
    }

    public Integer getStartHour() {
        return startHour;
    }

    public void setStartHour(Integer startHour) {
        this.startHour = startHour;
    }

    public Integer getStartMin() {
        return startMin;
    }

    public void setStartMin(Integer startMin) {
        this.startMin = startMin;
    }

    public Integer getEndHour() {
        return endHour;
    }

    public void setEndHour(Integer endHour) {
        this.endHour = endHour;
    }

    public Integer getEndMin() {
        return endMin;
    }

    public void setEndMin(Integer endMin) {
        this.endMin = endMin;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("idSubProject", "idProject", "idUser").toString();
    }
}
